package com.example.dllo.baidumusic.main;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

import com.example.dllo.baidumusic.R;
import com.example.dllo.baidumusic.adapter.MianAdapter;
import com.example.dllo.baidumusic.nettool.BaseFragment;
import com.example.dllo.baidumusic.fragment.MeFragment;
import com.example.dllo.baidumusic.fragment.MusicLibraryFragment;
import com.example.dllo.baidumusic.nettool.MyUser;

import java.util.ArrayList;

import cn.bmob.v3.BmobUser;

public class MainActivity extends BaseFragment implements View.OnClickListener {
    private ImageButton logh;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private MianAdapter adapter;
    private ImageButton search;
    private MyUser myUser;

    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        tabLayout = bindView(R.id.main_tablayout);
        viewPager = bindView(R.id.mian_viewpager);
        logh = bindView(R.id.mian_me_btn);
        search = bindView(R.id.mian_search_btn);
        search.setOnClickListener(this);
        logh.setOnClickListener(this);
        myUser = BmobUser.getCurrentUser(MyUser.class);

        if (myUser == null) {

            logh.setImageResource(R.mipmap.ic_baidu);
        } else {

            if (myUser.getIcon() == null) {
                logh.setImageResource(R.mipmap.ic_baidu);

            } else {
                logh.setImageBitmap(myUser.getIcon());


            }
        }
    }

    @Override
    protected void initData() {

        adapter = new MianAdapter(getChildFragmentManager());
        ArrayList<Fragment> fragments = new ArrayList<>();

        fragments.add(new MeFragment());
        fragments.add(new MusicLibraryFragment());

        adapter.setFragments(fragments);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mian_me_btn:
                Intent intent = new Intent(getContext(), UserLogihActivity.class);
                startActivity(intent);
                break;
            case R.id.mian_search_btn:
                Intent intentSearch = new Intent(getContext(), SearchActivity.class);
                startActivity(intentSearch);
        }
    }
}
